using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Collections;
using System.Globalization;
using System.IO;

namespace PopLava.Tools
{
    public partial class BackgroundImageDialog : Form
    {
        private XmlDocument g_xmlDocument;
        private string g_sTileName = String.Empty;
        private string g_sCurrentViewID = String.Empty;
        private string g_sXmlType = "Current Tile";
        private string g_sType = "Current Tile";
        private string g_sBackgroundImagePath = string.Empty;

        public BackgroundImageDialog(string sTileName, ref XmlDocument xmlSavedSettings, string sCurrentViewID, string sType)
        {
            InitializeComponent();
            g_sTileName = sTileName;
            g_xmlDocument = xmlSavedSettings;
            g_sCurrentViewID = sCurrentViewID;
            g_sType = sType;
            this.Text = this.Text + " (" + sType + ")";
            this.BackgroundImage_PictureBox.Image = GetSavedImage();
        }
        private Image GetSavedImage()
        {
            Image iImage = null;
            switch (g_sXmlType)
            {
                case "Current Tile":
                    {
                        XmlNodeList xmlViews = g_xmlDocument.DocumentElement.GetElementsByTagName("view");
                        foreach (XmlNode n1 in xmlViews)
                        {
                            if (n1.FirstChild.InnerText == g_sCurrentViewID)
                            {
                                XmlNodeList xmlTiles = n1.SelectNodes("tile");
                                foreach (XmlNode n2 in xmlTiles)
                                {
                                    if (n2.FirstChild.InnerText == g_sTileName)
                                    {
                                        XmlNode n3 = null;
                                        if (g_sType == "static")
                                        {
                                            n3 = n2.SelectSingleNode("backgroundimage");
                                            iImage = GetTileImage(n3.InnerText);
                                        }
                                        else if (g_sType == "hover")
                                        {
                                            n3 = n2.SelectSingleNode("hoverbackgroundimage");
                                            iImage = GetTileImage(n3.InnerText);
                                        }
                                        break;
                                    }
                                }
                                break;
                            }
                        }
                        break;
                    }
                case "Default For Current View":
                    {
                        XmlNodeList xmlViews = g_xmlDocument.DocumentElement.GetElementsByTagName("view");
                        foreach (XmlNode n1 in xmlViews)
                        {
                            if (n1.FirstChild.InnerText == g_sCurrentViewID)
                            {
                                XmlNode n2 = null;
                                if (g_sType == "static")
                                {
                                    n2 = n1.SelectSingleNode("defaultbackgroundimage");
                                    iImage = GetTileImage(n2.InnerText);
                                }
                                else if (g_sType == "hover")
                                {
                                    n2 = n1.SelectSingleNode("defaulthoverbackgroundimage");
                                    iImage = GetTileImage(n2.InnerText);
                                }
                                break;
                            }
                        }
                        break;
                    }
                case "Default For WinClickPro (All)":
                    {
                        XmlNode n1 = g_xmlDocument.DocumentElement; // WinClickPro
                        XmlNode n2 = null;
                        if (g_sType == "static")
                        {
                            n2 = n1.SelectSingleNode("defaultbackgroundimage");
                            iImage = GetTileImage(n2.InnerText);
                        }
                        else if (g_sType == "hover")
                        {
                            n2 = n1.SelectSingleNode("defaulthoverbackgroundimage");
                            iImage = GetTileImage(n2.InnerText);
                        }
                        break;
                    }
            }
            return iImage;
        }
        /// <summary>
        /// Saves the background image of a tile
        /// </summary>
        private void SaveTileBackgroundImage()
        {
            XmlHelper xmlHelper = new XmlHelper(ref g_xmlDocument);
            XmlNode n1 = xmlHelper.GetTileNode(g_sTileName, g_sCurrentViewID);
            XmlNode n2 = null;
            if (g_sType == "static")
            {
                n2 = n1.SelectSingleNode("backgroundimage");
            }
            else if (g_sType == "hover")
            {
                n2 = n1.SelectSingleNode("hoverbackgroundimage");
            }
            n2.InnerText = g_sBackgroundImagePath;
        }
        /// <summary>
        /// Saves the background image of a tile
        /// </summary>
        private void SaveViewBackgroundImage()
        {
            foreach (XmlNode xmlNodes in g_xmlDocument.DocumentElement.GetElementsByTagName("view"))
            {
                if (xmlNodes.FirstChild.InnerText == g_sCurrentViewID)
                {
                    foreach (XmlNode n1 in xmlNodes)
                    {
                        if (n1 == null || n1.Name.Length == 0)
                        {
                            MessageBox.Show("Fatal error occured while reading WinClickPro.xml (" + n1.Name + ")");
                            Environment.Exit(0);
                        }
                        if (g_sType == "static")
                        {
                            if (n1.Name == "defaultbackgroundimage")
                            {
                                n1.InnerText = g_sBackgroundImagePath;
                                break;
                            }
                        }
                        else if (g_sType == "hover")
                        {
                            if (n1.Name == "defaulthoverbackgroundimage")
                            {
                                n1.InnerText = g_sBackgroundImagePath;
                                break;
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Saves the background image of a tile
        /// </summary>
        private void SaveWinClickProBackgroundImage()
        {
            foreach (XmlNode n1 in g_xmlDocument.DocumentElement.ChildNodes)
            {
                if (g_sType == "static")
                {
                    if (n1.Name == "defaultbackgroundimage")
                    {
                        n1.InnerText = g_sBackgroundImagePath;
                        break;
                    }
                }
                else if (g_sType == "hover")
                {
                    if (n1.Name == "defaulthoverbackgroundimage")
                    {
                        n1.InnerText = g_sBackgroundImagePath;
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// Sets the image to null
        /// </summary>
        private void BackgroundImageRemove_Button_Click(object sender, EventArgs e)
        {
            this.BackgroundImage_PictureBox.Image = null;
            g_sBackgroundImagePath = String.Empty;
            switch (g_sXmlType)
            {
                case "Current Tile":
                    {
                        SaveTileBackgroundImage();
                        break;
                    }
                case "Default For Current View":
                    {
                        SaveViewBackgroundImage();
                        break;
                    }
                case "Default For WinClickPro (All)":
                    {
                        SaveWinClickProBackgroundImage();
                        break;
                    }
            }
        }
        /// <summary>
        /// Opens a find file dialog and saves the users image choice
        /// </summary>
        private void BackgroundImageChange_Button_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = Application.StartupPath + "\\Images";
            //
            //
            //
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                g_sBackgroundImagePath = ofd.FileName;
                this.BackgroundImage_PictureBox.Image = GetTileImage(g_sBackgroundImagePath);
                switch (g_sXmlType)
                {
                    case "Current Tile":
                        {
                            SaveTileBackgroundImage();
                            break;
                        }
                    case "Default For Current View":
                        {
                            SaveViewBackgroundImage();
                            break;
                        }
                    case "Default For WinClickPro (All)":
                        {
                            SaveWinClickProBackgroundImage();
                            break;
                        }
                }
            }
        }
        /// <summary>
        /// Restores the value to it's parent default.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BackgroundImageRestore_Button_Click(object sender, EventArgs e)
        {
            switch (g_sXmlType)
            {
                case "Current Tile":
                    {
                        RestoreTileBackgroundImage();
                        break;
                    }
                case "Default For Current View":
                    {
                        RestoreViewBackgroundImage();
                        break;
                    }
                case "Default For WinClickPro (All)":
                    {
                        RestoreRootBackgroundImage();
                        break;
                    }
            }
        }
        /// <summary>
        /// Restores the tile to the view default
        /// </summary>
        private void RestoreTileBackgroundImage()
        {
            XmlNodeList xmlViews = g_xmlDocument.DocumentElement.GetElementsByTagName("view");
            foreach (XmlNode n1 in xmlViews)
            {
                if (n1.FirstChild.InnerText == g_sCurrentViewID)
                {
                    XmlNode n2 = null;
                    if (g_sType == "static")
                    {
                        n2 = n1.SelectSingleNode("defaultbackgroundimage");
                    }
                    else
                    {
                        n2 = n1.SelectSingleNode("defaulthoverbackgroundimage");
                    }
                    XmlNodeList xmlTiles = n1.SelectNodes("tile");
                    foreach (XmlNode n3 in xmlTiles)
                    {
                        if (n3.FirstChild.InnerText == g_sTileName)
                        {
                            XmlNode n4 = null;
                            if (g_sType == "static")
                            {
                                n4 = n3.SelectSingleNode("backgroundimage");
                            }
                            else if (g_sType == "hover")
                            {
                                n4 = n3.SelectSingleNode("hoverbackgroundimage");
                            }
                            n4.InnerText = n2.InnerText;
                            this.BackgroundImage_PictureBox.Image = GetTileImage(n4.InnerText);
                            break;
                        }
                    }
                    break;
                }
            }
        }
        /// <summary>
        /// Restores the tile to the view default
        /// </summary>
        private void RestoreViewBackgroundImage()
        {
            XmlNode n1 = g_xmlDocument.DocumentElement;
            XmlNode n2 = null;
            if (g_sType == "static")
            {
                n2 = n1.SelectSingleNode("defaultbackgroundimage");
            }
            else
            {
                n2 = n1.SelectSingleNode("defaulthoverbackgroundimage");
            }
            XmlNodeList xmlViews = g_xmlDocument.DocumentElement.GetElementsByTagName("view");
            foreach (XmlNode n3 in xmlViews)
            {
                if (n3.FirstChild.InnerText == g_sCurrentViewID)
                {
                    XmlNode n4 = null;
                    if (g_sType == "static")
                    {
                        n4 = n3.SelectSingleNode("defaultbackgroundimage");
                    }
                    else if (g_sType == "hover")
                    {
                        n4 = n3.SelectSingleNode("defaulthoverbackgroundimage");
                    }
                    n4.InnerText = n2.InnerText;
                    this.BackgroundImage_PictureBox.Image = GetTileImage(n4.InnerText);
                    break;
                }
            }
        }
        /// <summary>
        /// Restores the tile to the view default
        /// </summary>
        private void RestoreRootBackgroundImage()
        {
            XmlNode n1 = g_xmlDocument.DocumentElement; // WinClickPro
            XmlNode n2;
            if (g_sType == "static")
            {
                n2 = n1.SelectSingleNode("defaultbackgroundimage");
                n2.InnerText = "";
                this.BackgroundImage_PictureBox.Image = GetTileImage(n2.InnerText);
            }
            else if (g_sType == "hover")
            {
                n2 = n1.SelectSingleNode("defaulthoverbackgroundimage");
                n2.InnerText = "";
                this.BackgroundImage_PictureBox.Image = GetTileImage(n2.InnerText);
            }
        }
        /// <summary>
        /// Returns an image object with or without an associated image
        /// If anything goes wrong, the default image will be returned
        /// Otherwise, we display the image at the specified path
        /// </summary>
        private static Image GetTileImage(string sImagePath)
        {
            Image image = null;
            //
            // Do we have a path?
            //
            if (sImagePath.Length > 0)
            {
                //
                // Can we find a file at the provided path?
                // If so, try to bring it in as an image.
                //
                if (File.Exists(sImagePath))
                {
                    try
                    {
                        image = Image.FromFile(sImagePath);
                    }
                    catch (Exception)
                    {
                        image = null; // The user will find out one way or another that they chose an invalid image type
                    }
                }
                else if (sImagePath == "[default]")
                {
                    image = global::PopLava.Tools.Properties.Resources.Default;
                }
            }

            return image;
        }
        private void Tile_RadioButton_Click(object sender, EventArgs e)
        {
            g_sXmlType = "Current Tile";
            this.BackgroundImage_PictureBox.Image = GetSavedImage();
        }

        private void View_RadioButton_Click(object sender, EventArgs e)
        {
            g_sXmlType = "Default For Current View";
            this.BackgroundImage_PictureBox.Image = GetSavedImage();
        }

        private void WinClickPro_RadioButton_Click(object sender, EventArgs e)
        {
            g_sXmlType = "Default For WinClickPro (All)";
            this.BackgroundImage_PictureBox.Image = GetSavedImage();
        }
    }
}